package com.liit.util;

import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;

import com.liit.entity.DictFormMap;

public class DictUtils {

	public static DictFormMap getDictById(Integer id) {
		Map<Object, Object> map = (Map<Object, Object>) EhcacheUtils.get("dict");
		return (DictFormMap) map.get(id);
	}

	public static List<DictFormMap> getDictByType(String type) {
		Map<Object, Object> map = (Map<Object, Object>) EhcacheUtils.get("dict");
		return (List<DictFormMap>) map.get(type);
	}

	/**
	 * 根据值查询
	 * 
	 * @param type
	 * @param value
	 * @return
	 */
	public static DictFormMap getDictByValue(String type, Integer value) {
		Map<Object, Object> map = (Map<Object, Object>) EhcacheUtils.get("dict");
		List<DictFormMap> list = (List<DictFormMap>) map.get(type);
		if (!CollectionUtils.isEmpty(list)) {
			for (DictFormMap dictFormMap : list) {
				if (dictFormMap.getInt("value").equals(value)) {
					return dictFormMap;
				}
			}
		}
		return null;
	}

	public static DictFormMap getFirstDict(String type) {
		Map<Object, Object> map = (Map<Object, Object>) EhcacheUtils.get("dict");
		return ((List<DictFormMap>) map.get(type)).get(0);
	}

	public static Integer getFirstDictValue(String type) {
		Map<Object, Object> map = (Map<Object, Object>) EhcacheUtils.get("dict");
		return ((List<DictFormMap>) map.get(type)).get(0).getInt("value");
	}

	public static String getFirstDictLabel(String type) {
		Map<Object, Object> map = (Map<Object, Object>) EhcacheUtils.get("dict");
		return ((List<DictFormMap>) map.get(type)).get(0).getStr("label");
	}
}
